<?php

    class BotDetector
    {
        private static $BOT_LIST = array(
            "Teoma",
            "alexa",
            "froogle",
            "inktomi",
            "looksmart",
            "URL_Spider_SQL",
            "Firefly",
            "NationalDirectory",
            "Ask Jeeves",
            "TECNOSEEK",
            "InfoSeek",
            "WebFindBot",
            "girafabot",
            "crawler",
            "www.galaxy.com",
            "Googlebot",
            "Scooter",
            "Slurp",
            "appie",
            "FAST",
            "WebBug",
            "Spade",
            "ZyBorg",
            "rabaz",
            "google",
            "yahoo"
        );

        private static $botSearchPattern = "";


        /**
         * Returns true if site is requested by bot.
         *
         * @return unknown
         */
        public static function IsBot()
        {
            $matches = array();
            $matchesCount = preg_match( self::GetBotSearchPattern(), strtolower( $_SERVER['HTTP_USER_AGENT'] ), $matches );

            return ( $matchesCount > 0 );
        }

        /**
         * Returns pattern for search bots.
         *
         * @return unknown
         */
        private static function GetBotSearchPattern()
        {
            if ( empty( self::$botSearchPattern ) )
            {
                $pattern = "/(";

                $botIndex = 0;
                foreach ( self::$BOT_LIST as $bot )
                {
                    $pattern .= $bot;
                    if ( $botIndex < count( self::$BOT_LIST ) - 1 )
                    {
                        $pattern .= " | ";
                    }
                    ++$botIndex;
                }
                $pattern .= ")/";

                self::$botSearchPattern = $pattern;
            }

            return self::$botSearchPattern;
        }
    }

?>